package com.situ.mall.common.controller;

import com.situ.mall.common.entity.Attachment;
import com.situ.mall.common.service.IAttachmentService;
import com.situ.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@Controller
@RequestMapping("/attachment")
public class AttachmentController {


    @Autowired
    private IAttachmentService attachmentService;


    @PostMapping("/upload")
    @ResponseBody
    public ResultVO upload(MultipartFile file) {
        return attachmentService.upload(file);
    }

    @PostMapping("/editorUpload")
    @ResponseBody
    public HashMap<String, Object> upload2(MultipartFile file) {
        ResultVO vo = upload(file);
        HashMap<String, Object> result = new HashMap<>();
        result.put("errno", 0);
        HashMap<String, Object> map = new HashMap<>();
        map.put("url", ((Attachment) vo.getData()).getFilePath());
        result.put("data", map);
        return result;
    }
}
